package com.lagou.sqlsession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XmlMapperBuilder {

    private Configuration configuration;

    public XmlMapperBuilder(Configuration configuration){
        this.configuration=configuration;
    }


    public  void parse(InputStream inputStream) throws DocumentException {
        Document document = new SAXReader().read(inputStream);
        Element rootElement = document.getRootElement();
        String namespce=rootElement.attributeValue("namespace");
        List<Element> list = rootElement.selectNodes("//select");
        for (Element element : list) {
            MappedStatement mappedStatement=new MappedStatement();
            String id=element.attributeValue("id");
            String statementid=namespce+"."+id;
            String resultType=element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql=element.getTextTrim();
            mappedStatement
                    .setStatememtid(id)
                    .setSql(sql)
                    . setParamterType(paramterType)
                    .setResultType(resultType);
            mappedStatement.setSqlCommandType(SqlCommandType.SELECT);
            configuration.getStatementMap().put(statementid,mappedStatement);
        }


        list = rootElement.selectNodes("//insert");
        for (Element element : list) {
            MappedStatement mappedStatement=new MappedStatement();
            String id=element.attributeValue("id");
            String statementid=namespce+"."+id;
            String resultType=element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql=element.getTextTrim();
            mappedStatement
                    .setStatememtid(id)
                    .setSql(sql)
                    . setParamterType(paramterType)
                    .setResultType(resultType);
            mappedStatement.setSqlCommandType(SqlCommandType.INSERT);
            configuration.getStatementMap().put(statementid,mappedStatement);
        }

        list = rootElement.selectNodes("//update");
        for (Element element : list) {
            MappedStatement mappedStatement=new MappedStatement();
            String id=element.attributeValue("id");
            String statementid=namespce+"."+id;
            String resultType=element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql=element.getTextTrim();
            mappedStatement
                    .setStatememtid(id)
                    .setSql(sql)
                    . setParamterType(paramterType)
                    .setResultType(resultType);
            mappedStatement.setSqlCommandType(SqlCommandType.UPDATE);
            configuration.getStatementMap().put(statementid,mappedStatement);
        }


        list = rootElement.selectNodes("//delete");
        for (Element element : list) {
            MappedStatement mappedStatement=new MappedStatement();
            String id=element.attributeValue("id");
            String statementid=namespce+"."+id;
            String resultType=element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String sql=element.getTextTrim();
            mappedStatement
                    .setStatememtid(id)
                    .setSql(sql)
                    . setParamterType(paramterType)
                    .setResultType(resultType);
            mappedStatement.setSqlCommandType(SqlCommandType.DELETE);
            configuration.getStatementMap().put(statementid,mappedStatement);
        }
    }
}
